//
//  UIImage+Base64.m
//  LuxuryGoods
//
//  Created by 刘星辰 on 2018/5/19.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "UIImage+Base64.h"

@implementation UIImage (Base64)
- (BOOL)isHasAlpha
{
    CGImageAlphaInfo alpha = CGImageGetAlphaInfo(self.CGImage);
    return (alpha == kCGImageAlphaFirst ||
            alpha == kCGImageAlphaLast ||
            alpha == kCGImageAlphaPremultipliedFirst ||
            alpha == kCGImageAlphaPremultipliedLast);
}

- (NSString *)base64EncodedString
{
    NSData *imageData = nil;
    NSString *mimeType = nil;
    
    if ([self isHasAlpha]) {
        imageData = UIImagePNGRepresentation(self);
        mimeType = @"image/png";
    } else {
        imageData = UIImageJPEGRepresentation(self, 0.2f);
        mimeType = @"image/jpeg";
    }
    
    return [imageData base64EncodedStringWithOptions:0];
}

- (UIImage * )compressImageWithQuality:(CGFloat)quality
{
    NSData *data = UIImageJPEGRepresentation(self, quality);
    return [UIImage imageWithData:data];
    
}
@end
